package servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import bdd.beans.DBLink;
import bdd.beans.Ec;
import bdd.beans.Enseignant;
import bdd.beans.ListeEnseignants;
import bdd.beans.ListeTypes;
import bdd.beans.Type;
import bdddaosimpl.DAOEcImpl;
import bdddaosimpl.DAOEnseignantImpl;
import bdddaosimpl.DAOTypeImpl;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author maboisba
 */
public class ListeEnseignantsServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String url = request.getRealPath("WEB-INF/prop.properties");
    out.println("DANS DOGET");
    DAOEnseignantImpl e = new DAOEnseignantImpl();
    List<Enseignant> l = e.findByEnseignantPoste();
    //ListeEnseignants listeEnseignants = new ListeEnseignants();
   //listeEnseignants.recupererListeEnseignants();
    //request.setAttribute("listeEnseignants", listeEnseignants);
   

    request.setAttribute("listeEnseignants", l);

    // On re-route l'appel vers la page d'accueil JSP
    gotoPage("/gestion_ec.jsp", request, response);
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
        try {
            response.setContentType("text/html;charset=UTF-8");
            DBLink db = new DBLink();
            db.connexion();
            PrintWriter out = response.getWriter();
            int numeroEns;
            int numeroType;
            int numeroEc;
            int nbHeures;
            numeroEns = Integer.parseInt(request.getParameter("numero_enseignant"));
            numeroType = Integer.parseInt(request.getParameter("types"));
            numeroEc = Integer.parseInt(request.getParameter("ecs"));
            nbHeures = Integer.parseInt(request.getParameter("nbHeures"));
            out.println(numeroEns + " " + numeroType + " " + numeroEc + " " + nbHeures);
            String rq = "SELECT JNTTYPEECENSEIGNANT.numenseignant, JNTTYPEECENSEIGNANT.numtype, JNTTYPEECENSEIGNANT.numec, "
                    + "JNTTYPEECENSEIGNANT.nbheures as nbheurestypeec FROM JNTTYPEECENSEIGNANT WHERE "
                    + "JNTTYPEECENSEIGNANT.numenseignant = ? AND JNTTYPEECENSEIGNANT.numtype = ? "
                    + "AND JNTTYPEECENSEIGNANT.numec = ?";
            PreparedStatement pst = db.getConnection().prepareStatement(rq);
            pst.setInt(1, numeroEns);
            pst.setInt(2, numeroType);
            pst.setInt(3, numeroEc);
            db.setPst(pst);
            ResultSet rs = db.getPst().executeQuery();
            boolean trouve = false;
            while(rs.next()){
                trouve = true;
                int heures = rs.getInt("nbheurestypeec");
                out.println("resultat trouve " + rs.getInt("nbheurestypeec"));
                String rqUpdate = "UPDATE JNTTYPEECENSEIGNANT SET nbheures = ? WHERE numenseignant = ? AND numtype = ? AND"
                        + "numec = ?";
                PreparedStatement pstUpdate = db.getConnection().prepareStatement(rqUpdate);
                pstUpdate.setInt(1, heures + nbHeures);
                pstUpdate.setInt(2, numeroEns);
                pstUpdate.setInt(3, numeroType);
                pstUpdate.setInt(4, numeroEc);
                pstUpdate.executeUpdate();
                break;
            }
            if(trouve = false){
                String rqInsert = "INSERT INTO JNTTYPEECENSEIGNANT VALUES ";
                PreparedStatement pstInsert = db.getConnection().prepareStatement(rqInsert);
                pstInsert.setInt(1, numeroEns);
                pstInsert.setInt(2, numeroType);
                pstInsert.setInt(3, numeroEc);
                pstInsert.setInt(4, nbHeures);
                pstInsert.execute();
            }

            db.getConnection().close();
            //gotoPage("/gestion_ec.jsp", request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ListeEnseignantsServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
  }

  private void gotoPage(String adress, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(adress);
    dispatcher.forward(request,response);
  }

}
